<script setup>
  import { Lock, User } from '@element-plus/icons-vue'
  import { ref, watch } from 'vue'
  import router from '@/router/index.js'
  import { useAccountStore } from '@/stores/modules/account.js'
  import { ElMessage } from 'element-plus'
  import { loginService, registerService } from '@/api/account.js'

  const accountStore = useAccountStore()
  const isRegister = ref(false)
  const form = ref()

  const formModel = ref({
    username: '',
    password: '',
    repeatPassword: ''
  })

  const rules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      {
        pattern: /^[a-zA-Z0-9_]{5,16}$/,
        message: '用户名只允许字母、数字、下划线,长度在5到16个字符之间',
        trigger: 'blur'
      }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 12, message: '密码长度在6到12个字符之间', trigger: 'blur' }
    ],
    repeatPassword: [
      { required: true, message: '请再次输入密码', trigger: 'blur' },
      { min: 6, max: 12, message: '密码长度在6到12个字符之间', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value !== formModel.value.password) {
            callback(new Error('两次输入密码不一致!'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
  }

  const register = () => {
    form.value.validate(async isValid => {
      if (isValid) {
        const res = await registerService(formModel.value)
        if (res.status) {
          ElMessage.success('注册成功')
          isRegister.value = false
        }
      }
    })
  }

  watch(isRegister, () => {
    formModel.value = {
      username: '',
      password: '',
      repeatPassword: ''
    }
  })

  const login = () => {
    form.value.validate(async isValid => {
      if (isValid) {
        const res = await loginService(formModel.value)
        if (res.status) {
          accountStore.setAccount(res.data)
          ElMessage.success('登录成功')
          await router.push('/')
        }
      }
    })
  }
</script>

<template>
  <el-row class="login-page" justify="center">
    <el-col :span="5" class="form">
      <el-form
        v-if="isRegister"
        ref="form"
        :model="formModel"
        :rules="rules"
        autocomplete="off"
        size="large"
      >
        <el-form-item>
          <div style="width: 100%; text-align: center; color: #213547; font-size: 22px"
            ><h1>Register</h1>
          </div>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repeatPassword">
          <el-input
            v-model="formModel.repeatPassword"
            :prefix-icon="Lock"
            placeholder="请输入再次密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button auto-insert-space class="button" color="#42b883" @click="register">
            立即注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link :underline="false" type="info" @click="isRegister = false"> ← 返回</el-link>
        </el-form-item>
      </el-form>

      <el-form v-else ref="form" :model="formModel" :rules="rules" autocomplete="off" size="large">
        <el-form-item>
          <div style="width: 100%; text-align: center; color: #213547; font-size: 22px"
            ><h1>Login</h1>
          </div>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            name="password"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button auto-insert-space class="button" color="#42b883" @click="login"
            >立即登录
          </el-button>
        </el-form-item>
        <el-form-item>
          <div class="flex">
            <el-link :underline="false" type="info" @click="isRegister = true"> 注册 →</el-link>
            <el-link :underline="false" type="info"> 忘记密码？</el-link>
          </div>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="less" scoped>
  .login-page {
    height: 100vh;

    .form {
      display: flex;
      flex-direction: column;
      justify-content: center;
      user-select: none;

      .title {
        margin: 0 auto;
      }

      .button {
        color: #fff;
        font-size: 16px;
        font-weight: 600;
        width: 100%;
      }

      .flex {
        width: 100%;
        display: flex;
        justify-content: space-between;
      }
    }
  }
</style>
